import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['STHeiti', 'PingFang SC', 'Heiti TC', 'Microsoft YaHei', 'SimHei', 'Arial Unicode MS']  
plt.rcParams['axes.unicode_minus'] = False

# 读取 CSV 文件
df = pd.read_csv("data-2.csv")  # 替换为你的文件名
df.set_index("scenario", inplace=True)
df.index = ["随到随充", "有序充电", "车网互动"]

# 准备绘图参数
years = df.columns
scenarios = df.index
x = np.arange(len(years))  # 每一组的位置
width = 0.25  # 每个柱子的宽度
colors = {
    "随到随充": "#a6eb9a",   # 蓝色
    "有序充电": "#f2a93b",   # 橙色
    "车网互动": "#685cc6"    # 绿色
}

fig, ax = plt.subplots(layout='constrained', figsize=(10, 6), dpi=300)

# 绘制每个 scenario 的柱子
for i, scenario in enumerate(scenarios):
    offset = width * i
    values = df.loc[scenario]
    rects = ax.bar(
        x + offset,
        values,
        width,
        label=scenario,
        color=colors[scenario],
        edgecolor='white',     # 加白色边框
        linewidth=1            # 边框宽度（可以调）
    )

# V2G数据
v2g_years = [2040, 2045, 2050]
v2g_values = [2.26, 2.28, 1.37]

# 在车网互动的柱子上方添加点和标注
v2g_x_positions = x[3:] + width * 2  # 车网互动是第三个场景，所以用width * 2
v2g_y_positions = df.loc["车网互动"].values[3:]  # 获取车网互动最后三年的值

# 绘制散点和标注
for x_pos, y_pos, v2g_val in zip(v2g_x_positions, v2g_y_positions, v2g_values):
    ax.scatter(x_pos, y_pos, color='red', s=100, zorder=3)
    ax.annotate(f'{v2g_val}', 
                xy=(x_pos, y_pos),
                xytext=(0, 10),
                textcoords='offset points',
                ha='center',
                va='bottom')

# 设置图表属性
ax.set_ylabel('Installed Capacity (亿千瓦时)')
ax.set_xticks(x + width, years)
ax.legend(loc='upper left')
ax.set_ylim(0, df.values.max() * 1.2)

plt.savefig('installed_capacity_with_v2g.png', bbox_inches='tight', dpi=300)
plt.show()
